using System;
using System.Collections.Generic;
using System.Text;

namespace Slf.Samples.FilteringAndTransformation
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.ForegroundColor = ConsoleColor.Green;
      Console.Out.WriteLine("The DecoratorLogger allows you to preprocess LogItems before they are forwarded to an underlying logger.");
      Console.Out.WriteLine("This sample filters all items with low log levels, and adds a prefix to warnings and errors.\n\n");
      Console.ResetColor();

      //create the logger that eventually receives output:
      ILogger target = new ConsoleLogger();

      //create a DecoratorLogger that filters / transforms LogItems:
      ILogger logger = new DecoratorLogger(target, FilterAndTransformItems);
      LoggerService.SetLogger(logger);

      CreateLogEntries();
      Console.ReadLine();
    }


    private static void CreateLogEntries()
    {
      ILogger logger = LoggerService.GetLogger();

      //the first two message will not be forwarded to the ConsoleLogger:
      logger.Debug("This is debug output.");
      logger.Info("This is an information.");

      //these messages will be logged:
      logger.Warn("This is a warning.");
      logger.Error("This is an error.");
      logger.Fatal("This is a fatal error.");
    }



    //This method filters all items with a LogLevel Below Warning
    //Other messages are being transformed with a prefix
    private static bool FilterAndTransformItems(LogItem item)
    {
      switch(item.LogLevel)
      {
        case LogLevel.Undefined:
        case LogLevel.Debug:
        case LogLevel.Info:
          //returning false causes the logged item to be discarded
          return false;
        case LogLevel.Warn:
          //prefix warning message
          item.Message = "WARNING: " + item.Message;
          return true;
        case LogLevel.Error:
          //prefix error message
          item.Message = "ERROR: " + item.Message;
          return true;
        case LogLevel.Fatal:
          //prefix fatal error message
          item.Message = "FATAL ERROR: " + item.Message;
          return true;
        default:
          throw new ArgumentOutOfRangeException("item", "Received item with unknown LogLevel");
      }
    }
  }
}